var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../../common/common');
var DBConfig = require('../../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);


// 用户地址
router.get('/jkyx/address_list', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	if (account_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入用户id',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getAddressByAccountID,[account_id],function (err, results) {
//			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var address = results;
				var address_len = address.length;
				if (address_len == 0) {
					res.end(JSON.stringify({
		     			msg: '操作成功',
		     			status:'100',
		     			address: []
		     		}));	    
				} else{
					var sql = ''
					for (var i = 0; i < address_len; i ++) {
						var value = 'select * from region where region_id in ('
						+ results[i].province_id  + ','
						+ results[i].city_id  + ','
						+ results[i].areas_id  + ','
						+ results[i].area_id  + ');';
						sql = sql + value;
					}
					connection.query(sql,function (err, results) {
						connection.release();
						if (err){
						  	res.end(JSON.stringify({
				     			msg: '数据库查询失败',
				     			status:'102',
				     			err: err
				     		}));
						}else{
							var useraddress = [];
							var region_len = results.length;
							if (address_len <= 1) {
								for (var i = 0; i < address_len; i++) {
								var item = {};
								item.id = address[i].id;
								item.account_id = address[i].account_id;
								item.consignee = address[i].consignee;
								item.mobile_number = address[i].mobile_number;
								item.address = address[i].address;
								item.type = address[i].type;  // 1: 家庭 2: 公司
								item.is_default = address[i].is_default; // 1: 默认 2: 不是默认
								item.province = {
									region_id: address[i].province_id,
									region_name: results[0].region_name
								}
								item.city = {
									region_id: address[i].city_id,
									region_name: results[1].region_name
								}
								item.areas = {
									region_id: address[i].areas_id,
									region_name: results[2].region_name
								}
								item.area = {
									region_id: address[i].area_id,
									region_name: results[3].region_name
								}
								useraddress.push(item);
							}
							} else{
								for (var i = 0; i < address_len; i++) {
									var item = {};
									item.id = address[i].id;
									item.account_id = address[i].account_id;
									item.consignee = address[i].consignee;
									item.mobile_number = address[i].mobile_number;
									item.address = address[i].address;
									item.type = address[i].type;  // 1: 家庭 2: 公司
									item.is_default = address[i].is_default; // 1: 默认 2: 不是默认
									item.province = {
										region_id: address[i].province_id,
										region_name: results[i][0].region_name
									}
									item.city = {
										region_id: address[i].city_id,
										region_name: results[i][1].region_name
									}
									item.areas = {
										region_id: address[i].areas_id,
										region_name: results[i][2].region_name
									}
									item.area = {
										region_id: address[i].area_id,
										region_name: results[i][3].region_name
									}
									useraddress.push(item);
								}
							}
							res.end(JSON.stringify({
				     			msg: '操作成功',
				     			status:'100',
				     			address: useraddress,
				     		}));	         
						}
					})
				}      
			}
		})
	})
});
// 武汉市 区 信息列表
router.get('/jkyx/address_region', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query('select * from region where parent_id = 180;',function (err, results) {
//			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var area = results;
				var area_len = area.length;
				connection.query(DBConfig.getRegionList,function (err, results) {
					if (err){
					  	res.end(JSON.stringify({
			     			msg: '数据库查询失败',
			     			status:'102',
			     			err: err
			     		}));
					}else{
						var aeras = results;
						var aeras_len = aeras.length;
						var regions = [];
						for (var i = 0; i < area_len; i ++) {
							var item = {};
							item.id = area[i].region_id;
							item.name = area[i].region_name;
							var tempaeras = [];
							for (var j = 0; j < aeras_len; j ++) {
								if (area[i].region_id == aeras[j].parent_id) {
									tempaeras.push({
										id: aeras[j].region_id,
										name: aeras[j].region_name,
									})
								}
							}
							if (tempaeras.length > 0) {
								item.areas = tempaeras;
								regions.push(item);
							} 
						}
						res.end(JSON.stringify({
			     			msg: '操作成功',
			     			status:'100',
			     			regions: regions
			     		}));	         
					}
				})      
			}
		})
	})
});
// 武汉市 街道 信息列表
router.get('/jkyx/address_jiedao', function(req, res, next) {
	var param = req.query || req.params; 
	var parent_id = param.parent_id;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getJiedao,[parent_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			jiedao: results
	     		}));	     
			}
		})
	})
});
// 设置默认地址
router.get('/jkyx/address_default', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	// 地址ID
	var address_id = param.address_id || '';
	if (account_id == '' || address_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.updateReceviceDefault,[account_id,account_id,address_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
// 删除地址
router.get('/jkyx/address_delete', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	// 地址ID
	var address_id = param.address_id || '';
	if (account_id == '' || address_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.deleteRecevice,[account_id,address_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
// 修改地址
router.get('/jkyx/address_edit', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	// 地址ID
	var address_id = param.address_id || '';
	// 收货人名字
	var consignee = param.consignee || '';
	// 收货人手机号
	var mobile_number = param.mobile_number || '';
	// 区
	var areas_id = param.areas_id || ''; 
	// 街道
	var area_id = param.area_id || ''; 
	// 详细地址
	var address = param.address || ''; 
	
	if (account_id == '' || address_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.editAddress,[consignee,mobile_number,areas_id,area_id,address,account_id,address_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
// 新增地址
router.get('/jkyx/address_add', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	// 收货人名字
	var consignee = param.consignee || '';
	// 收货人手机号
	var mobile_number = param.mobile_number || '';
	// 区
	var areas_id = param.areas_id || ''; 
	// 街道
	var area_id = param.area_id || ''; 
	// 详细地址
	var address = param.address || ''; 
	
	if (account_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		var timer = CommonJS.timestamp();
		connection.query(DBConfig.newAddress,[account_id,consignee,areas_id,area_id,mobile_number,address,timer],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});


module.exports = router;
